package cn.bonoon.kernel.web.handlers;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.descriptors.DescriptorFactory;
import cn.bonoon.kernel.descriptors.ObjectDescriptor;
import cn.bonoon.kernel.web.annotations.grid.AsDataGrid;
import cn.bonoon.kernel.web.annotations.grid.AsTreeGrid;
import cn.bonoon.kernel.web.controllers.GridButtonResolver;
import cn.bonoon.kernel.web.controllers.GridToolbarResolver;
import cn.bonoon.kernel.web.html.grid.DatagridBuilder;
import cn.bonoon.kernel.web.html.grid.StandardGridBuilder;
import cn.bonoon.kernel.web.html.grid.TreegridBuilder;

public abstract class AbstractHandlerRegister implements HandlerRegister{
	
	protected final StandardGridBuilder gridBuilder;
	protected final StandardAutoManager initializer;
//	private int dialogHeight;
	
	protected AbstractHandlerRegister(String id, Class<?> itemClass, StandardAutoManager initializer) throws Exception{
		this.initializer = initializer;
		ObjectDescriptor od = DescriptorFactory.objectDescriptor(itemClass);
		AsTreeGrid treeGrid = od.getAnnotation(AsTreeGrid.class);
		if(null != treeGrid){
			gridBuilder = new TreegridBuilder(treeGrid, od, initializer);
		}else{
			AsDataGrid dataGrid = od.getAnnotation(AsDataGrid.class);
			if(null != dataGrid){
				gridBuilder = new DatagridBuilder(dataGrid, od, initializer);
			}else{
				gridBuilder = new DatagridBuilder(od, initializer);
			}
		}
//		this.gridBuilder = gridBuilder;
//		ObjectDescriptor od = DescriptorFactory.objectDescriptor(itemClass);
//		AsTreeGrid treeGrid = od.getAnnotation(AsTreeGrid.class);
//		if(null != treeGrid){
//			ExecutableHandler handler;
//			if(objectDescriptor.isAssignable(AjaxNode.class)){
//				handler = new AjaxNodeLoaderHandler<E>(searchService, objectDescriptor.toClass());	
//			}else{
//				handler = new NodeLoaderHandler<E>(searchService, objectDescriptor.toClass());
//			}
//			initializer.register(handler);
//			return handler.getUrl();
//			
//			this.gridBuilder = new TreegridBuilder(od, initializer);
//		}else{
//			AsDataGrid dataGrid = od.getAnnotation(AsDataGrid.class);
//			Class<?> condition = null;
//			if(null != dataGrid){
//				condition = dataGrid.condition();
//				if(Object.class.equals(condition)){
//					condition = PageCondition.class;
//				}
//			}else{
//				condition = PageCondition.class;
//			}
//			this.gridBuilder = new DatagridBuilder(od, initializer);
//		}
	}
	
	@Override
	public String refreshParentComponenet() {
		return gridBuilder.getGridReload();
	}
	
	@Override
	public GridToolbarResolver toolbar() {
		return gridBuilder.addToolbar();
	}
	
	@Override
	public GridButtonResolver button() {
		return gridBuilder.addButton();
	}
	
	@Override
	public StandardAutoManager getManager() {
		return initializer;
	}
	
	public StandardGridBuilder getGridBuilder() {
		return gridBuilder;
	}
//	
//	@Override
//	public int getDialogHeight() {
//		return dialogHeight;
//	}
//	
//	public void setDialogHeight(int dialogHeight) {
//		this.dialogHeight = dialogHeight;
//	}
}
